import { queryVehicleDetail } from '../../services/vehicle'
import { uploadChargeDetectInfo, updateInstInfo } from '../../services/inspect'
import { queryInstDetailInfo } from '../../services/project'
import dayjs from 'dayjs'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:"",
    orderId:"",
    vehicleId:"",
    instId:"",
    vehicleInfo: {},
    spaceId:"",
    instInfo:{},
    showDialogAlert: false,
    dialogMsg:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(!options){
      return
    }
    let type = options.type
    let orderId = options.orderId
    let vehicleId = options.vehicleId
    let instId = options.instId
    
    if(type == 2){
      this.setData({
        showDialogAlert: true,
        dialogMsg:"请确认车辆电源已关闭!"
      })
    }

    this.setData({
      type,
      orderId,
      vehicleId,
      instId
    })
    if(vehicleId){
      this.getVehicleDetail(vehicleId)
    }
    if(instId){
      this.getInstDetailInfo(instId)
    }



  },

  onShow(){
    let orderId = this.data.orderId
    let type = this.data.type
    let spaceId = wx.getStorageSync(`${orderId}-${type}-spaceId`) || ""
    this.setData({
      spaceId
    })
  },

  async getInstDetailInfo(instId){
    let res = await queryInstDetailInfo(instId)
    console.log("ressss>>", res)
    if(res.code == 200){
      let instInfo = res.data
      this.setData({
        instInfo
      })
    }
  },

  async getVehicleDetail(vehicleId){
    let res = await queryVehicleDetail(vehicleId)
    if(res.code === 200){
      let vehicleInfo = res.data
      this.setData({
        vehicleInfo
      })      
    }
  },

  async handleUploadChargeDetectInfo(data){
    let res = await uploadChargeDetectInfo(data)
    console.log("res12222>>>", res)
  },

  handleNext(){
    let url = ""
    let type = this.data.type
    let orderId = this.data.orderId
    let vehicleId = this.data.vehicleId
    let instId = this.data.instId
    let code = wx.getStorageSync(`${orderId}-${type}-spaceId`) || ''

    if(code){
      url = `/pages/inspect-weather/inspect-weather?type=${type}&orderId=${orderId}&vehicleId=${vehicleId}&instId=${instId}&spaceId=${code}`
      wx.navigateTo({
        url,
      })
    }else {
      const _this = this
      wx.scanCode({
        success (res) {
          let codeRes = res.result
          const qList = codeRes.split('/')
          let spaceId = qList[qList.length - 1]
          console.log("spaceId>>>", spaceId)
          wx.setStorageSync(`${orderId}-${type}-spaceId`, spaceId)
          wx.removeStorageSync(`${orderId}-${type}-yaokong`)
          wx.removeStorageSync(`${orderId}-${type}-doCommandData`)
          if(type == 3){
            wx.removeStorageSync(`${orderId}-charging-init-start-soc`)
          }
          _this.handleUploadChargeDetectInfo({
            spaceId,
            instId,
          })
          _this.handleUpdateInstInfo({
            id: instId,
            detectStartTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
            detectEndTime: null
          })
          url = `/pages/inspect-weather/inspect-weather?type=${type}&orderId=${orderId}&vehicleId=${vehicleId}&instId=${instId}&spaceId=${spaceId}`
          wx.navigateTo({
            url,
          })
        }
      })
    }
  },

  handleScan(){
    let url = ""
    let type = this.data.type
    let orderId = this.data.orderId
    let vehicleId = this.data.vehicleId
    let instId = this.data.instId

    const _this = this
      wx.scanCode({
        success (res) {
          let codeRes = res.result
          const qList = codeRes.split('/')
          let spaceId = qList[qList.length - 1]
          console.log("spaceId>>>", spaceId)
          wx.setStorageSync(`${orderId}-${type}-spaceId`, spaceId)
          wx.removeStorageSync(`${orderId}-${type}-yaokong`)
          wx.removeStorageSync(`${orderId}-${type}-doCommandData`)
          if(type == 3){
            wx.removeStorageSync(`${orderId}-charging-init-start-soc`)
          }
          _this.handleUploadChargeDetectInfo({
            spaceId,
            instId,
          })
          _this.handleUpdateInstInfo({
            id: instId,
            detectStartTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
            detectEndTime: null
          })
          url = `/pages/inspect-weather/inspect-weather?type=${type}&orderId=${orderId}&vehicleId=${vehicleId}&instId=${instId}&spaceId=${spaceId}`
          wx.navigateTo({
            url,
          })
        }
      })
  },

  async handleUpdateInstInfo(info){
    let res = await updateInstInfo(info)
  }


})